<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Model\CarOil;

class OilController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    //

    public function all(){

        $all = CarOil::all();

        echo json_encode( $all );
    }

    public function getList( Request $request ){

        $res = [];
        $get = $request->all();
        $page = isset( $get['page'] )? intval($get['page']) : 1;
        $number = isset( $get['number'] )? intval($get['number']) : 20;
        $where = [];

        if( !empty( $get['name'] ) ){
            $where[] =[ 'name', 'like', '%' . $get['name'] .'%'];
        }



        $list = CarOil::where( $where )
            ->forPage($page, $number)
//            ->select()
            ->get();

        $count = CarOil::where( $where )
            ->count();

        $res = [
            'status'=>0,
            'total'=> $count,
            'page_total'=> ceil( $count / $number),
            'list'=> $list
        ];
        echo json_encode( $res );

    }


    public function single( $id ){

        $model = CarOil::where('car_oil.id', $id )

            ->select(
                'car_oil.id',
                'car_oil.name',
                'car_oil.img_url',
                'car_oil.link'
            )
            ->first();
        echo json_encode( $model );
    }

    /**
     * 获取brand 根据brand_id
     */
    public function edit( Request $request, $id ){

        $post = $request->all();
        unset( $post['api_token']);

        

        $data = CarOil::edit($id, $post);
        echo json_encode( $data );
    }

    public function delete( Request $request ){
        $post = $request->all();
        $oil = CarOil::find( $post['id'] );
        $res = $oil->delete();
        echo json_encode( $res );
    }

}
